local M = require "packages/mamashengde/record/Made"

local sushou = fk.CreateSkill {
  name = "shine__sushou",
}

Fk:loadTranslationTable {
  ["shine__sushou"] = "夙守",
  [":shine__sushou"] = "当你变身后，你可以重新分配与你距离为1的角色的座次，"..
  "然后消耗任意魔力，令等量名角色受到的火焰伤害+1。",

  ["#shine__sushou-invoke"] = "夙守：你可以分配与你距离为1角色的座次",
  ["$SuShou"] = "夙守",
  ["#shine__sushou-fire"] = "夙守：你可以消耗魔力令等量名角色受到火焰伤害+1",
  ["@shine__sushou2"] = "受到火伤+",
}

sushou:addEffect(M.HenshinEntered, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return data and data.who == player and player:hasSkill(sushou.name) and
      table.find(player.room.alive_players, function(p)
        return p:distanceTo(player) == 1
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player,{
      skill_name = sushou.name,
      prompt = "#shine__sushou-invoke"
    }) then
      local availablePlayerIds = table.map(table.filter(room.players, function(p)
        return p.rest > 0 or not p.dead
      end), Util.IdMapper)
      local disabledPlayerIds = {}
      disabledPlayerIds = table.filter(availablePlayerIds, function(pid)
        local p = room:getPlayerById(pid)
        return p:distanceTo(player) ~= 1
      end)
      local result = room:askToCustomDialog(player, {
        skill_name = sushou.name,
        qml_path = "packages/mobile/qml/TaMoBox.qml",
        extra_data = {
          availablePlayerIds,
          disabledPlayerIds,
          "$SuShou",
        },
      })
      if result ~= "" then
        event:setCostData(self, {extra_data = result})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local players = table.simpleClone(room.players)
    for seat, playerId in pairs(event:getCostData(self).extra_data) do
      players[seat] = room:getPlayerById(playerId)
    end
    room.players = players
    local player_circle = {}
    for i = 1, #room.players do
      room.players[i].seat = i
      table.insert(player_circle, room.players[i].id)
    end
    for i = 1, #room.players - 1 do
      room.players[i].next = room.players[i + 1]
    end
    room.players[#room.players].next = room.players[1]
    room:setCurrent(room.players[1])
    room:doBroadcastNotify("ArrangeSeats", player_circle)

    if not (player.dead or M.getMaryoku(player) == 0) then
      local tos = room:askToChoosePlayers(player, {
        targets = room.players,
        min_num = 1,
        max_num = M.getMaryoku(player),
        skill_name = sushou.name,
        prompt = "#shine__sushou-fire"
      })
      if #tos > 0 then
        M.changeMaryoku(player, -#tos, sushou.name)
        for _,to in ipairs(tos) do
          room:addPlayerMark(to, "@shine__sushou2")
        end
      end
    end
  end,
})

sushou:addEffect(fk.DamageInflicted, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@shine__sushou2") > 0 and data.damageType == fk.FireDamage
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data:changeDamage(player:getMark("@shine__sushou2"))
  end,
})

return sushou